import java.util.Scanner;

//商品
class Product{
    String name;
    String category;
    double price;
    int reserve;
//空参
    public Product(){
    }
//实参
    public Product(String name, String category, double price,int reserve){
        this.name = name;
        this.price = price;
        this.category = category;
        this.reserve = reserve;
    }

    public int getReserve() {
        return reserve;
    }

    public void setReserve(int reserve) {
        this.reserve = reserve;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }
}
class shoppingCart {
    Product[] products;
    int size;
    int capacity;//容量

    //空参
    public shoppingCart(int i) {

    }

    //实参
    public shoppingCart(int size, int capacity) {
        this.size = 0;
        this.capacity = capacity;
        this.products = new Product[capacity];
    }

    //添加商品
    public boolean addProduct(Product product, int count) {
        if (size + count > capacity) {
            System.out.println("购物车已满");
            return false;
        }
        for (int i = 0; i < count; i++) {
            if (product.getReserve() <= 0) {
                System.out.println("Not enough stock for " + product.getName());
                return false;
            }
            products[size++] = product;
            product.setReserve(product.getReserve() - 1);
        }
        return true;
    }

    //查看
    public void checkChat() {
        if (size == 0) {
            System.out.println("购物车为空");
        } else {
            System.out.println("Products in the cart:");
            for (int i = 0; i < size; i++) {
                System.out.println(products[i].getName() + "元" + products[i].getPrice());
            }
        }
    }

    //移除商品
    public boolean removeProduct(Product product) {
        for (int i = 0; i < size; i++) {
            if (products[i] == product) {
                for (int j = i; j < size - 1; j++) {
                    products[j] = products[j + 1];
                }
                products[size - 1] = null;
                return true;
            }
        }
        System.out.println("商品不存在");
        return false;
    }

    //结算
    public void settle() {
        double totalPrice = 0;
        for (int i = 0; i < size; i++) {
            totalPrice += products[i].getPrice();
        }
        System.out.println("总共为： 元" + totalPrice);
    }

    //清空
    public void clearChat() {
        products = new Product[capacity];
        size = 0;
    }
}

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Product[] allProducts = {
                new Product("雪碧", "饮料", 1.5, 10),
                new Product("咖啡", "饮料", 1.75, 10),
                new Product("果汁", "饮料", 2.0, 10),
                new Product("苹果", "水果", 0.5, 20),
                new Product("香蕉", "水果", 0.3, 30)
        };
        shoppingCart shoppingCart = new shoppingCart(5);
        boolean shopping=true;
        while (shopping) {
            System.out.println("1. 商品概览");
            System.out.println("2. 加入购物车");
            System.out.println("3. 查看购物车");
            System.out.println("4. 结算");
            System.out.println("5. 退出");
            System.out.print("Choose an option: ");
            int option = sc.nextInt();
            switch (option) {
                case 1:
                    for (Product product : allProducts) {
                        System.out.println(product.getName() + " " + product.getPrice() + " (类型: " + product.getCategory() + ")");
                    }
                    break;
                    case 2:
                        System.out.println("输入要加入的商品：");
                        for (Product product : allProducts) {
                            if(product.getName().equalsIgnoreCase(product.name)){
                                System.out.print("数量: ");
                                int count = sc.nextInt();
                                sc.nextLine();
                                if (!shoppingCart.addProduct(product,count)) {
                                    System.out.println("添加失败");
                                }
                                break;
                            }
                        }
                        break;
                case 3:
                    shoppingCart.checkChat();
                case 4:
                 shoppingCart.settle();
                    break;
                case 5:
                    shopping = false;
                    break;
                default:
                    System.out.println("选项不存在");
                    break;
            }
        }
    }
}

